﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core.Extensions;
using Microhard.Plat.CRM.Customers.Validator;
using Microhard.Plat.CRM.Customers.Sys;

namespace Microhard.Plat.CRM.Customers.Models
{
    [FluentValidation.Attributes.Validator(typeof(CustomerOrderModelValidator))]
    public class OrderModel : BasePermissionModel
    {
        public OrderModel()
        {
            this.DeliveryDate = DateTime.Now;
            this.IsSendCoin = false;
        }

        [ModelPKID]
        public Guid OrderID { set; get; }
        public Guid CompanyID { set; get; }
        public string Code { set; get; }
        public DateTime OrderDate { set; get; }
        public string OrderStore { set; get; }
        public string DeliveryAddress { set; get; }
        public decimal Amount { set; get; }
        public DateTime CreateTime { set; get; }
        public int Status { set; get; }
        public string StatusDisplay
        {
            get
            {
                string txt = string.Empty;
                switch (this.Status)
                {
                    case (int)OrderStatus.Save: txt = OrderStatus.Save.GetDisplayText(); break;
                    case (int)OrderStatus.Checked: txt = OrderStatus.Checked.GetDisplayText(); break;
                    case (int)OrderStatus.Delivering: txt = OrderStatus.Delivering.GetDisplayText(); break;
                    case (int)OrderStatus.Finished: txt = OrderStatus.Finished.GetDisplayText(); break;
                }
                return txt;
            }
        }
        public DateTime DeliveryDate { set; get; }
        public string HQCompanyCode { set; get; }
        public Guid SelectKAIDForQuery { set; get; }
        public TimeSpan TakeTime
        {
            get
            {
                if (DeliveryDate != DateTime.MinValue)
                {
                    var ts = DeliveryDate - OrderDate;
                    return ts;
                }
                return new TimeSpan();
            }
        }
        public string IsDelivery
        {
            get
            {
                return this.DeliveryDate != null ? "是" : "否";
            }
        }

        public string OrderDescription
        {
            get
            {

                string orderNotifTemplate = "订单号:{0},类型:{1},状态:{2},订货日期:{3},订货地点:{4},库存地:{5},金额:{6}";
                return string.Format(orderNotifTemplate, this.Code, this.OrderTypeDisplay, this.StatusDisplay, this.OrderDate, this.OrderStore, this.DeliveryAddress, this.Amount);
            }
        }

        public List<OrderDetailModelInServer> Detials { set; get; }
        public Guid CustomerID { set; get; }
        public int OrderType { set; get; }
        public OrderType OrderTypeSelect { set; get; }
        public string OrderTypeDisplay
        {
            get
            {
                switch (this.OrderType)
                {
                    case (int)Microhard.Plat.CRM.Customers.Sys.OrderType.CustomerOrder:
                        return Microhard.Plat.CRM.Customers.Sys.OrderType.CustomerOrder.GetDisplayText();
                    case (int)Microhard.Plat.CRM.Customers.Sys.OrderType.KAOrder:
                        return Microhard.Plat.CRM.Customers.Sys.OrderType.KAOrder.GetDisplayText();
                    case (int)Microhard.Plat.CRM.Customers.Sys.OrderType.Promotion:
                        return Microhard.Plat.CRM.Customers.Sys.OrderType.Promotion.GetDisplayText();
                }
                return string.Empty;
            }
        }
        public Guid FromKAID { set; get; }

        public string QueryCustomerName { set; get; }
        public string QueryKAName { set; get; }
        public string QueryAddress { set; get; }
        public DateTime QueryDeliveryDate { set; get; }


        public CustomerModel Customer { set; get; }
        public string SubKAName { set; get; }
        public bool IsSendCoin { set; get; }
        public string IsSendCoinDisplay
        {
            get
            {
                return IsSendCoin == true ? "已赠送" : "未赠送";
            }
        }
    }

}
